package org.liuwei.concucrent.base03.RentrantLock03;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
/**
 * 
* @Title: ReentrantLockTime.java 
* @Package org.liuwei.concucrent.base03.ReentrantLock01 
* @Description: TODO(可限时间) 
* @author 刘伟  15818570028@163.com   
* @date 2016年8月20日 下午8:38:38 
* @version V1.0
 */
public class ReentrantLockTime03 implements Runnable {
	public static ReentrantLock lock = new ReentrantLock();
	public static int count = 0;

	public void run() {

		for (int i = 0; i < 10000; i++) {
			try {
				lock.tryLock(5, TimeUnit.SECONDS);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
			try {
				count++;
			}finally {
				if(lock.isHeldByCurrentThread())
				lock.unlock();
			}
		}
	}

	public static void main(String[] args) throws InterruptedException {
		ReentrantLockTime03 r = new ReentrantLockTime03();
		Thread t = new Thread(r);
		Thread t2 = new Thread(r);
		t.start();
		t2.start();
		t.join();
		t2.join();
		System.out.println(count);
	}

}
